package com.itic.mall.mobile.msg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.internal.lang.annotation.ajcDeclareAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.type.ReadType;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.msg.apppush.persistence.model.AppPush;
import com.itic.mall.system.msg.apppush.persistence.model.AppPushExample;
import com.itic.mall.system.msg.apppush.service.AppPushService;
import com.itic.mall.system.msg.type.persistence.model.MsgType;
import com.itic.mall.system.msg.type.service.MsgTypeService;
import com.itic.mall.system.users.usersmall.persistence.model.UsersMall;
@Controller
@RequestMapping(value = "/mobile/msg")
public class MsgInterface extends BaseMobileInterface{
	
	@Autowired
	private AppPushService appPushService;
	@Autowired
	private MsgTypeService msgTypeService;
	@RequestMapping(value="type") 
	@ResponseBody
	public Map<String, Object> getMsgType(HttpServletRequest request,HttpServletResponse response,String parentId,String token,String appId){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try{
			if(StringUtils.isNoneBlank(appId,token)){
				UsersMall usersMall = getUsersByTokenAndAppId(token, appId);
				if(usersMall != null){
					List<MsgType> msgTypes = this.msgTypeService.getParentMsgType();
					List<Map<String, Object>> list = new ArrayList<>();
					for(MsgType type :msgTypes) {
						Map<String, Object> dataMap = new HashMap<>();
						dataMap.put("id", type.getId());
						dataMap.put("icon", type.getIconPath());
						dataMap.put("name", type.getName());
						dataMap.put("type", type.getType());
						int count = this.appPushService.getNotReadCountByTypeId(type,usersMall.getId());
						dataMap.put("count", count);
						list.add(dataMap);
					}
					resultMap.put(DATA, list);
					errorCode = SUCCESS_ERROR_CODE;
					errorMsg = SUCCESS_ERROR_MSG;
				}else{
					errorCode = LOGIN_ERROR_CODE;
					errorMsg = LOGIN_ERROR_MSG;
				}
			}
		}catch(Exception e){
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("MsgInterface", "getMsgType", e.toString(), "用户消息类型", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
		
	
		
		
	} 
	
		
	@RequestMapping(value="list")
	@ResponseBody
	public Map<String ,Object> userMsgList(HttpServletRequest request,HttpServletResponse response,String parentId,String token,String appId,String start,String length){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try{
			if(StringUtils.isNoneBlank(appId,token)){
				UsersMall usersMall = getUsersByTokenAndAppId(token, appId);
				if(usersMall != null){
					AppPushExample example = new AppPushExample();
					this.handlePageQueryDefault(request, example);
					List<MsgType> msgTypes = this.msgTypeService.getMsgTypeByParentId(parentId);
					List<String> types = new ArrayList<>();
					for(MsgType msgType:msgTypes)
						types.add(msgType.getId());
					List<AppPush> appPushs = this.appPushService.getMsgListByUserId(usersMall.getId(), example,types);
					int count = this.appPushService.getCountByUserId(usersMall.getId());
					List<Map<String, Object>> listMap = new ArrayList<>();
					for(AppPush push: appPushs) {
						Map<String, Object> dataMap = new HashMap<>();
						MsgType msgType = this.msgTypeService.get(push.getType());
						dataMap.put("id", push.getId());
						dataMap.put("createTime",push.getCreateTime());
						dataMap.put("isRead",push.getIsRead());
						dataMap.put("type", msgType.getType());
						dataMap.put("content", push.getPushText());
						dataMap.put("name", msgType.getName());
						listMap.add(dataMap);
					}
					Map<String, Object> map = new HashMap<>();
					map.put("list", listMap);
					map.put("total", count);
					resultMap.put(DATA, map);
					errorCode = SUCCESS_ERROR_CODE;
					errorMsg = SUCCESS_ERROR_MSG;
				}else{
					errorCode = LOGIN_ERROR_CODE;
					errorMsg = LOGIN_ERROR_MSG;
				}
			}
		}catch(Exception e){
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("MsgInterface", "userMsgList", e.toString(), "用户消息列表", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
		
	}
	
	@RequestMapping(value="info")
	@ResponseBody
	public Map<String ,Object> msgInfo(HttpServletRequest request,HttpServletResponse response,String msgId){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try{
			if(StringUtils.isNoneBlank(msgId)){
				Map<String, Object> dataMap = new HashMap<>();
				AppPush push = this.appPushService.get(msgId);
				if(ReadType.UnRead.getState().equals(push.getIsRead())) {
					push.setIsRead(ReadType.Read.getState());
					this.appPushService.edit(push);
				}
				MsgType msgType = this.msgTypeService.get(push.getType());
				dataMap.put("id", push.getId());
				dataMap.put("id", push.getId());
				dataMap.put("createTime",push.getCreateTime());
				dataMap.put("type", msgType.getType());
				dataMap.put("content", push.getPushText());
				dataMap.put("name", msgType.getName());
				resultMap.put(DATA, dataMap);
				errorCode = SUCCESS_ERROR_CODE;
				errorMsg = SUCCESS_ERROR_MSG;
			}
		}catch(Exception e){
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("MsgInterface", "msgInfo", e.toString(), "消息详情", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	} 
	
	@RequestMapping(value="del")
	@ResponseBody
	public Map<String, Object> delMsgById(HttpServletResponse response ,String token,String appId,String msgId){

		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try{
			if(StringUtils.isNoneBlank(appId,token,msgId)){
				AppPush push = this.appPushService.get(msgId);
				String result = SUCCESS;
				if(push!=null) {
					push.setIsEffective(DBConst.FALSE);
					result = this.appPushService.edit(push);
				}
				resultMap.put(DATA, result);
				errorCode = SUCCESS_ERROR_CODE;
				errorMsg = SUCCESS_ERROR_MSG;
			}
		}catch(Exception e){
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("MsgInterface", "delMsgById", e.toString(), "删除消息", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
		
	}
}
